﻿Imports System.IO
Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.Data
Imports System.Data.OleDb

Public Class rssFeed : Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        'send document as response
        context.Response.ContentType = "text/xml"
        context.Response.ContentEncoding = System.Text.Encoding.UTF8
        context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(3600))
        context.Response.Cache.SetCacheability(HttpCacheability.Public)



        'create RSS xml document
        Dim xml As New XmlTextWriter(context.Response.OutputStream, Encoding.UTF8)
        xml.Formatting = Formatting.Indented

        xml.WriteStartDocument()
        xml.WriteRaw(vbCrLf)
        xml.WriteRaw("<?xml-stylesheet type=""text/css"" href=""rss.css""?>")
        xml.WriteStartElement("rss")
        xml.WriteAttributeString("version", "2.0")

        'create feed header section
        xml.WriteStartElement("channel")
        xml.WriteElementString("title", "Photo Mania")
        xml.WriteElementString("link", "../Default.aspx")
        xml.WriteElementString("description", "Latest Pictures")

        'get latest recipes
        Dim ds As DataSet = GetArticles()

        'add stories to feed
        Dim row As DataRow
        For Each row In ds.Tables("RSSContent").Rows
            xml.WriteStartElement("item")
            xml.WriteElementString("title", row("caption"))
            xml.WriteElementString("link", Replace(HttpContext.Current.Request.Url.AbsoluteUri(), HttpContext.Current.Request.Url.AbsolutePath(), "") & "\" & row("imagelocation"))
            xml.WriteElementString("description", "Uploaded By " & row("name"))
            xml.WriteStartElement("image")
            xml.WriteElementString("url", Replace(HttpContext.Current.Request.Url.AbsoluteUri(), HttpContext.Current.Request.Url.AbsolutePath(), "") & "\" & row("imagelocation"))
            xml.WriteElementString("title", row("caption"))
            xml.WriteElementString("link", Replace(HttpContext.Current.Request.Url.AbsoluteUri(), HttpContext.Current.Request.Url.AbsolutePath(), "") & "\" & row("imagelocation"))
            xml.WriteEndElement()
            xml.WriteEndElement()



        Next

        'close channel
        xml.WriteEndElement()
        'close rss
        xml.WriteEndElement()
        'close document
        xml.WriteEndDocument()
        xml.Flush()
        xml.Close()

    End Sub

    Private Function GetArticles() As DataSet

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("photodb").ConnectionString)

        Dim SqlString As String = "SELECT ALL name,imagelocation,caption from gallery"

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)

        cmd.CommandType = CommandType.Text

        Dim ds As New DataSet

        oleDbConn.Open()
        Dim da As New OleDbDataAdapter(cmd)
        da.Fill(ds, "RSSContent")
        Dim maxRows As Integer
        maxRows = ds.Tables("RSSContent").Rows.Count
        oleDbConn.Close()
        cmd.Dispose()
        oleDbConn.Dispose()

        Return ds
    End Function

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class